﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EmptoolWebApp.Helpers
{
    public class CompanyHelper : BaseHelper
    {
        public static string GetAssociationTypeName(int t)
        {
            return Entities.CompanyAssociationType.GetName(t);
        }

        public static string GetCompanyName(string companyPK)
        {
            string companyName = string.Empty;

            try
            {
                Microsoft.WindowsAzure.Storage.Table.CloudTable tbl =
                Helpers.AzureStorageHelper.GetStorageTable(Entities.CompanyEntity.TABLE_NAME);

                Microsoft.WindowsAzure.Storage.Table.TableOperation retrieveOperation =
                    Microsoft.WindowsAzure.Storage.Table.TableOperation.Retrieve<EmptoolWebApp.Entities.CompanyEntity>(companyPK, string.Empty);

                Microsoft.WindowsAzure.Storage.Table.TableResult retrievedResult = tbl.Execute(retrieveOperation);
                EmptoolWebApp.Entities.CompanyEntity companyEntity = retrievedResult.Result as EmptoolWebApp.Entities.CompanyEntity;
                if (companyEntity != null)
                {
                    return companyEntity.CompanyName;
                }
            }
            catch (Exception)
            {
                throw;
            }

            return companyName;
        }

        public static string GetRelationship(string companyPK, string userId)
        {
            string relationship = "UNRELATED";

            try
            {
                Microsoft.WindowsAzure.Storage.Table.CloudTable tbl =
                Helpers.AzureStorageHelper.GetStorageTable(Entities.CompanyAssociateEntity.TABLE_NAME);

                Microsoft.WindowsAzure.Storage.Table.TableOperation retrieveOperation =
                    Microsoft.WindowsAzure.Storage.Table.TableOperation.Retrieve<EmptoolWebApp.Entities.CompanyAssociateEntity>(companyPK,
                    Helpers.CryptographyHelper.GetMD5Hash(userId));

                Microsoft.WindowsAzure.Storage.Table.TableResult retrievedResult = tbl.Execute(retrieveOperation);
                EmptoolWebApp.Entities.CompanyAssociateEntity companyAssociateEntity = retrievedResult.Result as EmptoolWebApp.Entities.CompanyAssociateEntity;
                if (companyAssociateEntity != null)
                {
                    relationship = CompanyHelper.GetAssociationTypeName(companyAssociateEntity.AssociationType);
                }
            }
            catch (Exception)
            {

                throw;
            }

            return relationship;
        }
    }
}
